<template>
  <div style="min-width: 484px">
    <fks-query-page
      v-loading="listLoading"
      :data="tableData"
      :total="total"
      :page-size.sync="pageSize"
      :current-page.sync="currentPage"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[20, 50, 100]"
      :element-loading-text="LANG.LOADING"
      :table-name="LANG.USER_SESSION_LIST"
      @query="getData"
      @sort-change="sortHandler"
    >
      <template>
        <fks-table-column type="index" align="center" label="#">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </fks-table-column>
        <fks-table-column
          prop="userName"
          :label="LANG.USERNAME"
          width="160"
          sortable="custom"
        >
          <template slot-scope="scope">
            <span>{{
              scope.row.userInfo
                ? scope.row.userInfo.userFullname
                : scope.row.userName
            }}</span>
          </template>
        </fks-table-column>
        <fks-table-column
          prop="sessionStartTime"
          :label="LANG.SESSION_START_TIME"
        />
        <fks-table-column
          prop="lastOperationalTime"
          :label="LANG.LATEST_OPERATE_TIME"
        />
        <fks-table-column prop="userName" :label="LANG.STATUS" width="160">
          <div slot-scope="scope">
            <fks-tag v-if="scope.row.webStatus != 0" type="info">
              {{ LANG.OFFLINE }}
            </fks-tag>
            <fks-tag v-if="scope.row.webStatus == 0" type="success">
              {{ LANG.ONLINE }}
            </fks-tag>
          </div>
        </fks-table-column>
        <fks-table-column :label="LANG.OPERATION" width="100" align="center">
          <template slot-scope="scope">
            <fks-button
              v-permission="['session:exit']"
              danger-text
              @click="exit(scope.row)"
            >
              {{ LANG.LOGOUT }}
            </fks-button>
          </template>
        </fks-table-column>
      </template>
    </fks-query-page>
  </div>
</template>
<script>
import Mix from '@/mixins/module'
import * as types from '@/store/Getter/getterTypes.js'
import * as user from './api'
export default {
  name: 'Session',
  mixins: [Mix],
  data() {
    return {
      pageSize: 20,
      currentPage: 1,
      total: 0,
      tableData: [],
      listLoading: false,
      sort: '',
      columnName: '',
      msgTemplateParam: {
        text: '您已被管理员下线，请点击确定 跳转到登录页',
        destination: '/topic/message'
      } //k-v值对
    }
  },
  created() {
    this.getData()
  },
  methods: {
    //会话列表排序
    sortHandler(data) {
      let params = {}
      if (!data.order) {
        this.sort = ''
        this.columnName = ''
      } else {
        this.sort = data.order == 'ascending' ? 'asc' : 'desc'
        this.columnName = 'user_name'
      }
      params = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        sort: this.sort,
        columnName: this.columnName
      }
      this.getSessionList(params)
    },
    //获取用户会话数据
    getData() {
      this.listLoading = true
      let params = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        sort: this.sort,
        columnName: this.columnName
      }
      this.getSessionList(params)
    },
    //获取用户会话数据
    getSessionList(params) {
      user
        .getData(params)
        .then((res) => {
          if (res.status && res.data) {
            this.tableData = res.data.list
            this.total = res.data.total
          }
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    //为登出用户发送登出消息
    postMsg(msgTemplateParam, userName) {
      let params = ''
      params = JSON.stringify(msgTemplateParam)
      let data = {
        msgSendType: 0,
        msgTemplateId: '1325679427776618498',
        msgTemplateParam: params,
        msgType: 400,
        toClientId: 'fawkes',
        toUsers: userName,
        fromTenantId: 100000,
        fromClientId: 'fawkes'
      }
      user.postMsg(data)
    },
    //登出用户
    exit(row) {
      this.$confirm(this.LANG.OFFIINE_USER, {
        title: this.LANG.TIPS,
        type: 'warning',
        confirmButtonText: this.LANG.CONFIRM,
        cancelButtonText: this.LANG.CANCEL,
        confirmButtonClass: 'fks-button--danger',
        deleteMode: true
      }).then(() => {
        user.exit(row.userName).then((res) => {
          if (res.status) {
            this.$message.success(this.LANG.OFFIINE_SUCCESS)
            this.getData()
            this.postMsg(this.msgTemplateParam, row.userName)
          }
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>